<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <input type="text" placeholder="请输入n的值" />
    <input type="text" placeholder="请输入a的值" />
    <button>计算</button>
    <span></span>
    <script>
      var oInput = document.getElementsByTagName("input")[0];
      var oInput2 = document.getElementsByTagName("input")[1];
      var oBtn = document.getElementsByTagName("button")[0];
      var oSpan = document.getElementsByTagName("span")[0];

      oBtn.onclick = function () {
        var val = oInput.value * 1;
        var val2 = oInput2.value * 1;
        var n;
        if (val < val2) {
          n = val;
        } else if (val2 < val) {
          n = val2;
        }
        for (var i = n; i > 0; i--) {
          if (val % i == 0 && val2 % i == 0) {
            oSpan.innerText=i
            break;
          }
        }
      };
    </script>
  </body>
</html>
